
*-------------------------------------------------------------------------------
* suburbs population data
*-------------------------------------------------------------------------------

import delimited "data/inputs/other/il cities population.csv", clear 

gen keep = 1 if name == "Aurora city" | ///
			    name == "Joliet city" | ///
				name == "Naperville city" | ///
				(name == "Waukegan city" & county == 0) | ///
				name == "Bolingbrook village" | ///
				name == "Elgin city" | ///
				(name == "Cicero town" & county == 0) | ///
				name == "Schaumburg village" | ///
				(name == "Evanston city" & county == 0) | ///
				(name == "Arlington Heights village" & county == 0) | ///
				(name == "Palatine village" & county == 0) | ///
				(name == "Oak Lawn village" & county == 0) | ///
				(name == "Berwyn city" & county == 0) | ///
				(name == "Mount Prospect village" & county == 0) | ///
				(name == "Wheaton city" & county == 0) | ///
				(name == "Tinley Park village" & county == 0) | ///
				(name == "Oak Park village" & county == 0) | ///
				(name == "Hoffman Estates village" & county == 0) | ///
				(name == "Downers Grove village" & county == 0) | ///
				(name == "Skokie village" & county == 0) | ///			
				(name == "Des Plaines city" & county == 0) | ///
				(name == "Orland Park village" & county == 0) | ///				
				(name == "Wheaton city" & county == 0) 								
				
qui su keep
assert `r(sum)' == 22

keep if keep == 1
keep name popestimate*

reshape long popestimate, i(name) j(year, string)
gen month = tm(2020m4) if year == "042020"
replace year = "01/" + year if length(year) == 4
replace month = monthly(year, "MY") if month == .
format month %tm

egen cityid = group(name)
tsset cityid month
tsfill, full

gen ln_pob = ln(pop)
by cityid: ipolate ln_pob month, gen(ln_pob_i)
gen pop_cgr = exp(ln_pob_i)

collapse (sum) pop_cgr, by(month)

rename pop_cgr pop_cgr_suburbs
tempfile pop_suburbs
savesome month pop using `pop_suburbs'


*-------------------------------------------------------------------------------
* chicago population data
*-------------------------------------------------------------------------------

* population
clear 
set obs 120
gen month = tm(2010m6) if _n == 1
replace month = month[_n-1] + 1 if month[_n-1] ~= .
format month %tm
gen pop = 2695598 if _n == 1
replace pop = 2746388 if month == tm(2020m5)

	/*
	Population, Census, April 1, 2020	2,746,388
	Population, Census, April 1, 2010	2,695,598
	*/
	
gen ln_pob = ln(pop)
ipolate ln_pob month, gen(ln_pob_i)
gen pop_cgr = exp(ln_pob_i)
tempfile pop_chicago
savesome month pop_cgr using `pop_chicago'


*-------------------------------------------------------------------------------
* traffic stop data
*-------------------------------------------------------------------------------

use "data/outputs/TSSS_beat.dta", clear

replace agencyname = "troopers" if regexm(agencyname, "ILLINOIS")
replace agencyname = "cpd" if regexm(agencyname, "CHICAGO")
replace agencyname = "suburbs" if agencyname ~= "troopers" & agencyname ~= "cpd"

collapse (sum) count* black citation reason_movingviolation reason_equip, by(agencyname month)

reshape wide count count_race black citation reason_movingviolation reason_equip, i(month) j(agencyname, string)
renvars count* black* citation*, prefix(itss)

drop if month < tm(2012m1) | month > tm(2020m2)


*-------------------------------------------------------------------------------
* merge
*-------------------------------------------------------------------------------

merge 1:1 month using `pop_chicago'
drop _m
merge 1:1 month using `pop_suburbs'
drop _m


*-------------------------------------------------------------------------------
* calculate quantities for graphs
*-------------------------------------------------------------------------------

* rates
gen stoprate_suburbs = 100 * 12 *itsscountsuburbs / pop_cgr_suburbs
gen stoprate_cpd = 100 * 12 * itsscountcpd / pop_cgr

* treatment area	   
gen treatment_rates = 29.2 if (month >= tm(2015m8) & month <= tm(2016m1))
gen treatment = 1 if (month >= tm(2015m8) & month <= tm(2016m1))


*-------------------------------------------------------------------------------
* graph
*-------------------------------------------------------------------------------
   
#delimit;
twoway (area treatment_rates month if month >= tm(2012m1) & month <= tm(2020m2), 
		lcolor(gs15%100) fcolor(gs15%100))
	(connected stoprate_suburbs month  if month >= tm(2012m1) & month <= tm(2020m2),
		lcolor(gs8) mcolor(gs8) msymbol(triangle) msize(vsmall))
	(connected stoprate_cpd month if month >= tm(2012m1) & month <= tm(2020m2), 
		lcolor(blue%80) lwidth(medthick) msymbol(square) mcolor(blue%80) msize(vsmall)), 
graphregion(fcolor(white) lcolor(white) margin(zero))
plotregion(fcolor(white) lstyle(none) lcolor(white) ilstyle(none))
xsize(7) ysize(5)
title("", 
	  color(black) placement(west) justification(left) size(medlarge)) 
ytitle("Stops per 100 per Year", size(medlarge))
yscale(lcolor(none))
ylabel(0(5)25, labsize(medlarge) glcolor(white) angle(horizontal))
xtitle("", size(medsmall)) 
xscale(lcolor(none))
xlabel(624(12)723, labsize(medlarge) format(%tmCY)) 
legend(off);
graph export "figures/FigureS3.pdf", replace;


* End
